import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'visitStatisticsM',
  state: {
    organizationId: '',
    getEchartsOne: { xAxis: [], serieslist: undefined },
    getEchartsTwo: { series: undefined },
    getEchartsThree: { list: undefined, size: undefined },
    getEchartsFour: { serieslist: [], xaxislist: undefined },
    loginTimesTop5: { yAxis: [], series: undefined },
    LoginTotalTimesTrend: [],
    totalNumOfLogins: { xAxis: [], series: undefined },
  },

  effects: {
    // 获取最早登录时点趋势
    *getEchartsOne({ payload, callback }, { call, put }) {
      const res = yield call(api.visitStatistics.getEchartsOne, payload);
      if (res && res.success == true) {
        yield put({
          type: 'save',
          payload: {
            getEchartsOne: res.data[0],
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            getEchartsOne: { xAxis: [], serieslist: [{ name: '', data: undefined }] },
          },
        });
        message.error(res?.message || '获取数据失败');
      }
    },

    // 获取
    *getEchartsTwo({ payload, callback }, { call, put }) {
      const res = yield call(api.visitStatistics.getEchartsTwo, payload);
      if (res && res.success == true) {
        yield put({
          type: 'save',
          payload: {
            getEchartsTwo: res.data,
          },
        });
      } else {
        // yield put({
        //   type: 'save',
        //   payload: {
        //     getEchartsTwo: {series:undefined},
        //   },
        // });
        message.error(res.message);
      }
    },

    //
    *getEchartsThree({ payload, callback }, { call, put }) {
      const res = yield call(api.visitStatistics.getEchartsThree, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            getEchartsThree: res.data.dataSource,
          },
        });
      } else {
        message.error(res.msg);
      }
    },

    //
    *getEchartsFour({ payload, callback }, { call, put }) {
      const res = yield call(api.visitStatistics.getEchartsFour, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            getEchartsFour: res.data.dataSource,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },

    //机构登录次数top5
    *getTopFiveLogins({ payload, callback }, { call, put }) {
      const res = yield call(api.visitStatistics.getTopFiveLogins, payload);
      if (res && res.code == 0) {
        const { yAxis = [] } = res.data;
        const { id, value } = yAxis[yAxis.length - 1];
        yield put({
          type: 'save',
          payload: {
            loginTimesTop5: res.data,
            organizationId: id,
            name: value,
          },
        });
        // callback(res,'success')
      } else {
        message.error('获取数据失败');
      }
    },

    //机构登录次数分布
    *getLoginFrequency({ payload, callback }, { call, put }) {
      const res = yield call(api.visitStatistics.getLoginFrequency, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            LoginTotalTimesTrend: res.data.series,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },

    //机构登录次数分布
    *getTotalNumOfLogins({ payload, callback }, { call, put }) {
      const res = yield call(api.visitStatistics.getTotalNumOfLogins, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            totalNumOfLogins: res.data,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
